package com.fuqi.android.zhongcai.base.view.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.fuqi.android.zhongcai.R;
import com.fuqi.android.zhongcai.application.UniApplication;
import com.fuqi.android.zhongcai.base.Constants;
import com.fuqi.android.zhongcai.base.view.adapter.BaseRecyclerViewAdapter;
import com.fuqi.android.zhongcai.base.view.adapter.OrderCustomerAdapter;
import com.fuqi.android.zhongcai.base.view.widget.CustomProgress;
import com.fuqi.android.zhongcai.business.shuig.request.api.OrderListApi;
import com.fuqi.android.zhongcai.business.shuig.request.result.OrderListResult;
import com.fuqi.android.zhongcai.business.shuig.view.activity.ShuiGActivity;
import com.fuqi.android.zhongcai.utils.ActivityUtils;
import com.fuqi.android.zhongcai.utils.LogUtils;
import com.fuqi.framework.gson.GsonHelper;
import com.fuqi.framework.network.code.HandleCode;
import com.fuqi.framework.network.http.RequestManager;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by Lenovo on 2016/8/29.
 */
public class OrderChildFragment extends BaseFragment {

    @Bind(R.id.recyclerview)
    XRecyclerView mRecyclerView;
    private OrderCustomerAdapter orderCustomerAdapter;
    private List<OrderListResult> orderList;
    private int currentPage = 1;
    private int showCount = 10;
    private String orderType;
//    private static OrderChildFragment fragment;

    public static OrderChildFragment getInstance(String orderType){
        /*if(fragment == null) fragment = new OrderChildFragment(orderType);
        return fragment;*/
        OrderChildFragment fragment = new OrderChildFragment(orderType);
        Bundle bdl = new Bundle();
        bdl.putString("orderType", orderType);
        fragment.setArguments(bdl);
        return fragment;
    }

    public OrderChildFragment() {
    }

    @SuppressLint({"NewApi", "ValidFragment"})
    public OrderChildFragment(String orderType) {
        this.orderType = orderType;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        orderType = getArguments().getString("orderType");
    }

    @Override
    public void initView() {

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        orderList = new ArrayList<>();
        orderCustomerAdapter = new OrderCustomerAdapter(getActivity(),orderList);
        mRecyclerView.setAdapter(orderCustomerAdapter);


        if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_GRKH)){
            // 用户

        } else if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_SGGR)){
            // 施工工人

        }else if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_JCY)){
            // 监察员

        }else if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_NQ)){
            // 内勤


        }else if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_SGDZ)){
            // 施工队长


        }


    }

    @Override
    public void initListener() {

        orderCustomerAdapter.setOnItemClickListener(new BaseRecyclerViewAdapter.OnItemClickListener<OrderListResult>() {
            @Override
            public void onItemClick(View view, int position, OrderListResult data) {

                Bundle bundle = new Bundle();
                bundle.putString("order_id",data.getOrderId());
                bundle.putString("order_type",data.getOrderType());
                ActivityUtils.startActivity(getActivity(),ShuiGActivity.class,bundle);

        }
        });


        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                CustomProgress.show(getActivity(), "正在处理...",true,null);
                RequestManager.getInstance().call(new OrderListApi(new OrderListApi.OrderListParams(orderType,
                        String.valueOf(showCount),
                        String.valueOf(currentPage)), respOrderListener, errorListener));
            }

            @Override
            public void onLoadMore() {
                currentPage ++;
                CustomProgress.show(getActivity(), "正在处理...",true,null);
                RequestManager.getInstance().call(new OrderListApi(new OrderListApi.OrderListParams(orderType,
                        String.valueOf(showCount),
                        String.valueOf(currentPage)), respOrderListener, errorListener));
            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();
//        initData();
        RequestManager.getInstance().call(new OrderListApi(new OrderListApi.OrderListParams(orderType,
                String.valueOf(showCount),
                String.valueOf(currentPage)), respOrderListener, errorListener));
    }

    @Override
    public void initData() {


//        if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_GRKH)){
//            // 用户
//
//        } else if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_SGGR)){
//            // 施工工人
//
//        }else if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_JCY)){
//            // 监察员
//
//        }else if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_NQ)){
//            // 内勤
//
//
//        }else if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_SGDZ)){
//            // 施工队长
//
//        }




    }


    Response.Listener<OrderListResult[]> respOrderListener = new Response.Listener<OrderListResult[]>() {

        @Override
        public void onResponse(OrderListResult[] response) {

            CustomProgress.close();
            if (HandleCode.requestSuccess()) {
                if(response.length == 0)
                    mRecyclerView.noMoreLoading();
                if(currentPage == 1 && orderList != null) {
                    mRecyclerView.setIsnomore(false);
                    orderList.clear();
                }
                for(int i = 0; i < response.length ;i++) {
                    orderList.add(response[i]);
                }

                orderCustomerAdapter.notifyDataSetChanged();
                LogUtils.i( "response OrderListResult: " + GsonHelper.writeValue(response));

            } else {

            }
            mRecyclerView.refreshComplete();
            mRecyclerView.loadMoreComplete();
        }
    };


    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            CustomProgress.close();
            mRecyclerView.refreshComplete();
            mRecyclerView.loadMoreComplete();
            Log.i("juner", "juner  error.getMessage(): " + error.getMessage());

        }
    };

    @Override
    public int getLayoutId() {
        return R.layout.fragment_order_child;
    }
}
